package com.heima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.reggie.common.R;
import com.heima.reggie.pojo.Category;
import com.heima.reggie.service.CategoryService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> tianjia(@RequestBody Category category) {

        categoryService.save(category);
        return R.success("成功");
    }

    @GetMapping("/page")
    public R<Page> select(Integer page, Integer pageSize) {
        Page<Category> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> lam = new LambdaQueryWrapper<>();
        lam.orderByAsc(Category::getSort);
        categoryService.page(page1, lam);


        return R.success(page1);
    }

    @PutMapping
    public  R<String>  update(@RequestBody Category category){
        boolean b = categoryService.updateById(category);
        return R.success("成功");


    }
@DeleteMapping
    public R<String> delete(Long id){
categoryService.delect(id);


        return R.success("删完了");
    }
 @GetMapping("/list")
    public R list(Category category){
     LambdaQueryWrapper<Category>  lam=new    LambdaQueryWrapper<Category>();
     lam.eq(category.getType()!=null,Category::getType,category.getType());
     lam.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

     List<Category> list = categoryService.list(lam);


     return R.success(list);
    }

}
